#!/bin/sh

# SPDX-License-Identifier: GPL-2.0
# Copyright (C) 2017-2018 Team LibreELEC (https://libreelec.tv)
# Copyright (C) 2018-present Team CoreELEC (https://coreelec.org)

display_mode=$(cat /sys/class/display/mode)

for arg in $(cat /proc/cmdline); do
  case ${arg} in
    bootfrom*)
      bootfromext=1
      ;;
    BOOT_IMAGE=*)
      BOOT_IMAGE=${arg#*=}
      ;;
    boot=*)
      boot=${arg#*=}
      ;;
    disk=*)
      disk=${arg#*=}
      ;;
    hdmimode=*)
      hdmimode=${arg#*=}
      ;;
  esac
done

# Add information where to run CoreELEC from
if [ -z "$BOOT_IMAGE" -o -z "$boot" -o -z "$disk" ]; then
  cmdline=$(cat /proc/cmdline)
  if [ -n "$bootfromext" ]; then
    cmdline="$cmdline BOOT_IMAGE=kernel.img boot=LABEL=@BOOT_LABEL@ disk=LABEL=@DISK_LABEL@"
  else
    cmdline="$cmdline BOOT_IMAGE=/dev/boot boot=/dev/system disk=/dev/data"
  fi
  echo "$cmdline" > /proc/cmdline
fi

# Enable HDMI output if cable is connected and not already enabled by u-boot
if [ "$(cat /sys/class/amhdmitx/amhdmitx0/hpd_state)" != "0" ] && [ "$display_mode" != "$hdmimode" ]; then
  echo "$hdmimode" > /sys/class/display/mode
  display_mode="$hdmimode"
fi

# Set framebuffer geometry to match the resolution
case $display_mode in
  480*)            X=720  Y=480  ;;
  576*)            X=720  Y=576  ;;
  720p*)           X=1280 Y=720  ;;
  *)               X=1920 Y=1080 ;;
esac

fbset -fb /dev/fb0 -g $X $Y 1920 2160 32
fbset -fb /dev/fb1 -g 32 32 32 32 32
echo 0 > /sys/class/graphics/fb0/free_scale
echo 0 > /sys/class/graphics/fb1/free_scale
echo 1 > /sys/class/video/disable_video

# Enable scaling for 4K output
case $display_mode in
  4k*|smpte*|2160*)
    echo 0 0 1919 1079 > /sys/class/graphics/fb0/free_scale_axis
    echo 0 0 3839 2159 > /sys/class/graphics/fb0/window_axis
    echo 1920 > /sys/class/graphics/fb0/scale_width
    echo 1080 > /sys/class/graphics/fb0/scale_height
    echo 0x10001 > /sys/class/graphics/fb0/free_scale
  ;;
esac

# Include deinterlacer into default VFM map
echo rm default > /sys/class/vfm/map
echo add default decoder ppmgr deinterlace amvideo > /sys/class/vfm/map

# Enable framebuffer device
echo 0 > /sys/class/graphics/fb0/blank

# Blank fb1 to prevent static noise
echo 1 > /sys/class/graphics/fb1/blank

# set smp_affinity
#            Binary       Hex
#    CPU 0    0001         1
#    CPU 1    0010         2
#    CPU 2    0100         4
#    CPU 3    1000         8
echo 2 > /proc/irq/231/smp_affinity  # hdmi_aocec
echo 4 > /proc/irq/63/smp_affinity   # dwc_otg, dwc_otg_hcd:usb1
echo 8 > /proc/irq/62/smp_affinity   # dwc_otg, dwc_otg_hcd:usb2, dwc_otg_pcd

# DEC_CONTROL_FLAG_DISABLE_FAST_POC
echo 4 > /sys/module/amvdec_h264/parameters/dec_control

echo 1 > /sys/devices/system/cpu/cpufreq/ondemand/io_is_busy

# S912: Set 1GHz minimum CPU speed for both clusters
if [ -f /proc/device-tree/t82x@d00c0000/compatible ] ; then
  echo 1000000 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq
  echo 1000000 > /sys/devices/system/cpu/cpu4/cpufreq/scaling_min_freq
fi
